package com.dinadi.yidi.ui.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.ChoiceAreaAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.AddressManagementEntity;
import com.dinadi.yidi.bean.AddressManagementRequest;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/11/23  地区选择
 * Time: 10:52
 */
public class ChoiceAreaActivity extends BaseActivity {
    private View view;
    private LinearLayout btn_go_back;
    private ListView lv_area;
    private ChoiceAreaAdapter adapter;
    private List<AddressManagementEntity> dataBean = new ArrayList<>();
    private int tag = 0;
    private String areaId, province, city, county;
    private String area = "";
    private String areaIds = "";

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(ChoiceAreaActivity.this).inflate(R.layout.activity_choice_area, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        btn_go_back = (LinearLayout) view.findViewById(R.id.btn_go_back);
        lv_area = (ListView) view.findViewById(R.id.lv_area);
        lv_area.setDivider(null);
        adapter = new ChoiceAreaAdapter(ChoiceAreaActivity.this, dataBean);
        lv_area.setAdapter(adapter);

    }

    @Override
    protected void initListener() {
        registerClickListener(btn_go_back);
        adapter.setOnClickListenerCallBack(new ChoiceAreaAdapter.OnClickListenerCallBack() {
            @Override
            public void onClickType(int position, List<AddressManagementEntity> stringList) {
                areaId = stringList.get(position).getAreaId();
                province = stringList.get(position).getAreaId();
                city = stringList.get(position).getAreaId();
                county = stringList.get(position).getAreaId();
                area += stringList.get(position).getName();
                areaIds += stringList.get(position).getAreaId() + ",";
                tag++;
                if (tag == 3) {
                    ChoiceAreaActivity.this.finish();
                    SharePreferencesHelper.getInstance(ChoiceAreaActivity.this).putString("area", area);
                    SharePreferencesHelper.getInstance(ChoiceAreaActivity.this).putString("areaIds", areaIds);
                } else {
                    adapter.clearData(dataBean);
                    getAreaDate();
                }

            }
        });
    }

    @Override
    protected void initData() {
        getAreaDate();
    }

    private void getAreaDate() {

        MessageBean bean = new MessageBean();
        bean.setAreaId(areaId);
        CommonInterface messageInterface = new CommonInterface(ChoiceAreaActivity.this, httpListener);
        messageInterface.getInterface("16017", JSON.toJSONString(bean));
    }


    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_go_back:
                ChoiceAreaActivity.this.finish();
                break;
            default:
                break;
        }

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            dataBean.clear();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                AddressManagementRequest bean = JsonUtil.parseJsonToBean(response, AddressManagementRequest.class);
                dataBean.addAll(bean.getAreaCodeList());
                adapter.notifyDataSetChanged();

            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

}
